package com.cuitrip.business.login.proxy;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.cuitrip.business.login.model.ThirdAccount;
import com.cuitrip.service.R;
import com.cuitrip.util.DialogUtil;
import com.cuitrip.util.ResourceUtil;
import com.lab.logtrack.AppLog;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.ToastUtil;
import com.mob.tools.utils.UIHandler;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.pedant.SweetAlert.ProgressHelper;
import cn.pedant.SweetAlert.SweetAlertDialog;
import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.google.GooglePlus;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * 第三方帐号授权
 * Created by ak on 15/9/23.
 */
public class ThirdAccountProxy implements Handler.Callback,
        PlatformActionListener {


    private OnAuthListener onAuthListener;

    private int accountType;

    private static final int MSG_USERID_FOUND = 1;
    private static final int MSG_LOGIN = 2;
    private static final int MSG_AUTH_CANCEL = 3;
    private static final int MSG_AUTH_ERROR = 4;
    private static final int MSG_AUTH_COMPLETE = 5;
    private ProgressDialog sweetAlertDialog;

    private Handler handler = new Handler();

    private SoftReference<Activity> mAcitivityReference;


    public void init(Activity activity) {
        mAcitivityReference = new SoftReference<Activity>(activity);
        ShareSDK.initSDK(mAcitivityReference.get());
        sweetAlertDialog = DialogUtil.createLoadingProgressDialog(mAcitivityReference.get(), true);
    }

    public void destroy() {
        ShareSDK.stopSDK();
    }

    public void authorizeThirdAccount(int type , OnAuthListener onAuthListener){
        this.accountType = type;
        this.onAuthListener = onAuthListener;
        if (mAcitivityReference.get() == null){
            return;
        }
        switch (type){
            case ThirdAccount.WE_CHAT:
                wechatLogin(mAcitivityReference.get());
                break;
            case ThirdAccount.WEIBO:
                weiboLogin(mAcitivityReference.get());
                break;
            case ThirdAccount.FACEBOOK:
                facebookLogin(mAcitivityReference.get());
                break;
            case ThirdAccount.GOOGLE:
                googleLogin(mAcitivityReference.get());
                break;
        }
    }

    private void wechatLogin(Context context) {
        Wechat wechat = new Wechat(context);
        authorize(wechat, true);
    }


    private void weiboLogin(Context context) {
        SinaWeibo weibo = new SinaWeibo(context);
        authorize(weibo, isWeiboInstall(context)?false:true);
    }

    private void facebookLogin(Context context) {
        Facebook facebook = new Facebook(context);
        authorize(facebook, false);
    }

    private void googleLogin(Context context) {
        GooglePlus googlePlus = new GooglePlus(context);
        authorize(googlePlus, true);
    }



    private void authorize(Platform plat, boolean isSSOSetting) {
        plat.removeAccount(true);
        plat.setPlatformActionListener(this);
        plat.SSOSetting(isSSOSetting);
        if (accountType == ThirdAccount.GOOGLE){
            plat.authorize();
        }else{
            plat.showUser(null);
        }
        sweetAlertDialog.show();
    }


    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_AUTH_CANCEL: {
                if (onAuthListener != null){
                    onAuthListener.onCancel();
                }
            }
            break;
            case MSG_AUTH_ERROR: {
                if (onAuthListener != null){
                    onAuthListener.onCancel();
                }
            }
            break;
            case MSG_AUTH_COMPLETE: {
                if (onAuthListener != null){
                    ThirdAccount thirdAccount = (ThirdAccount) msg.obj;
                    onAuthListener.onComplete(thirdAccount);
                }
            }
            break;
        }
        return false;
    }


    //在oncomplete中的hashmap返回用户信息
    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {

        if (action == Platform.ACTION_USER_INFOR) {
            Message msg = new Message();
            msg.what = MSG_AUTH_COMPLETE;
            ThirdAccount thirdAccount = new ThirdAccount();
            thirdAccount.setAccountType(accountType);
            thirdAccount.setDataParams(hashMap);
            msg.obj = thirdAccount;
            UIHandler.sendMessage(msg, this);
        }
        dismissDialog();
    }


    @Override
    public void onError(Platform platform, int action, Throwable throwable) {
        if (action == Platform.ACTION_USER_INFOR) {
            AppLog.e(throwable.toString());
            UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);

        }
        dismissDialog();

    }

    private void dismissDialog(){
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (sweetAlertDialog != null && sweetAlertDialog.isShowing()) {
                    sweetAlertDialog.dismiss();
                }

            }
        });
    }

    @Override
    public void onCancel(Platform platform, int action) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
        }
        dismissDialog();
    }

    public static boolean isQQInstalled(Context context){
        QQ plat = new QQ(context);
        return plat.isClientValid();
    }

    public static boolean isWechatInstall(Context context){
        Wechat plat = new Wechat(context);
        return plat.isClientValid();
    }

    public static boolean isGoogleInstall(Context context){
        GooglePlus plat = new GooglePlus(context);
        return plat.isClientValid();
    }

    public static boolean isWeiboInstall(Context context){
        SinaWeibo plat=new SinaWeibo(context);
        return plat.isClientValid();
    }


    public static boolean isFacebookInstall(Context context){
        Facebook plat = new Facebook(context);
        return plat.isClientValid();
    }

    public interface OnAuthListener {

        public void onComplete(ThirdAccount thirdAccount);

        public void onCancel();
    }

    public void onActivityStop(){
        dismissDialog();
    }

}
